import com.google.appengine.api.datastore.Entity
import com.google.appengine.api.datastore.Blob
import com.google.appengine.api.datastore.Key
import com.google.appengine.api.datastore.*
import static com.google.appengine.api.datastore.FetchOptions.Builder.*
import com.pobos.model.*


def sesion = request.getSession(true)
def usuario = sesion.getAttribute('usuario')

def idConcello = params.id

if (usuario) {	
	Entity concello = obtenConcello(idConcello)
	request['concello'] = concello
	forward '/admin/modificaConcello.gtpl'
} else {
	redirect '/login.gtpl'
}


def obtenUsuarioSesion(String usuarioSesion) {
	def query = new Query(Usuario.class.simpleName)
	query.addFilter("nome", Query.FilterOperator.EQUAL, usuarioSesion)
	def preparedQuery = datastore.prepare(query)
	
	if (preparedQuery.countEntities() == 0) {
		return null;
	} else {
		if(preparedQuery.countEntities() > 1){
			return preparedQuery.asList(withLimit(1))[0]
		}else{
			return preparedQuery.asSingleEntity()
		}
	}
	
}

def obtenConcello(def id) {
	def key = KeyFactory.createKey("Concello", Long.parseLong(id))			
	return datastore.get(key)
}  



